using System;
using System.Xml;

namespace Deepcode.SchedulerService
{
	/// <summary>
	/// Provides useful utilities for processing XML
	/// </summary>
	public class XmlUtilityService
	{
		#region Safe Attribute readers
		/// <summary>
		/// Interrogates Xml node node for the attribute. If the attribute could not be found,
		/// it returns the default value, otherwise returns the value of the attribute;
		/// </summary>
		/// <param name="node"></param>
		/// <param name="attribute"></param>
		/// <param name="defaultvalue"></param>
		/// <returns></returns>
		public string GetSafe( XmlNode node, string attribute, string defaultvalue )
		{
			string result = defaultvalue;
			try
			{
				result = node.Attributes[attribute].Value;
			}
			catch
			{
				result = defaultvalue;
			}
			return result;
		}
		/// <summary>
		/// Interrogates Xml node node for the attribute. If the attribute could not be found,
		/// or converted to the appropriate type, it returns the default value, otherwise 
		/// returns the value of the attribute as an int.
		/// </summary>
		/// <param name="node"></param>
		/// <param name="attribute"></param>
		/// <param name="defaultvalue"></param>
		/// <returns></returns>
		public int GetSafe( XmlNode node, string attribute, int defaultvalue )
		{
			string r = GetSafe( node, attribute, "");
			if( r == "" ) return defaultvalue;
			int result = defaultvalue;
			try{ result = Int32.Parse( r ); }
			catch{ result = defaultvalue; }
			return result;
		}
		/// <summary>
		/// Interrogates Xml node node for the attribute. If the attribute could not be found,
		/// or converted to the appropriate type, it returns the default value, otherwise 
		/// returns the value of the attribute as a bool.
		/// </summary>
		/// <param name="node"></param>
		/// <param name="attribute"></param>
		/// <param name="defaultvalue"></param>
		/// <returns></returns>
		public bool GetSafe( XmlNode node, string attribute, bool defaultvalue )
		{
			string r = GetSafe( node, attribute, "");
			if( r == "" ) return defaultvalue;
			bool result = defaultvalue;
			try{ result = bool.Parse( r ); }
			catch{ result = defaultvalue; }
			return result;
		}
		/// <summary>
		/// Interrogates Xml node node for the attribute. If the attribute could not be found,
		/// or converted to the appropriate type, it returns the default value, otherwise 
		/// returns the value of the attribute as a DateTime.
		/// </summary>
		/// <param name="node"></param>
		/// <param name="attribute"></param>
		/// <param name="defaultvalue"></param>
		/// <returns></returns>
		public DateTime GetSafe( XmlNode node, string attribute, DateTime defaultvalue )
		{
			string r = GetSafe( node, attribute, "");
			if( r == "" ) return defaultvalue;
			DateTime result = defaultvalue;
			try{ result = DateTime.Parse( r ); }
			catch{ result = defaultvalue; }
			return result;
		}
		/// <summary>
		/// Interrogates Xml node node for the attribute. If the attribute could not be found,
		/// or converted to the appropriate type, it returns the default value, otherwise 
		/// returns the value of the attribute as a GUID
		/// </summary>
		/// <param name="node"></param>
		/// <param name="attribute"></param>
		/// <param name="defaultvalue"></param>
		/// <returns></returns>
		public Guid GetSafe( XmlNode node, string attribute, Guid defaultvalue )
		{
			string r = GetSafe( node, attribute, "");
			if( r == "" ) return defaultvalue;
			Guid result = defaultvalue;
			try{ result = new Guid( r ); }
			catch{ result = defaultvalue; }
			return result;
		}
		#endregion
	}
}
